struct Solution;

// impl Solution {
//   pub fn truncate_sentence(s: String, k: i32) -> String {
//     let mut ret = String::new();
//     let mut start = 0;
//     let mut count = 0;
//     for (index, c) in s.chars().enumerate() {
//       if c == ' ' {
//         if count >= k {
//           break;
//         }
//         ret.push_str(&s[start..index]);
//         start = index;
//         count += 1;
//       }
//     }
//     if count < k {
//       ret.push_str(&s[start..]);
//     }  
//     return ret;
//   }
// }

impl Solution {
  pub fn truncate_sentence(s: String, k: i32) -> String {
    let mut ret = String::new();
    let mut count = 0;
    for c in s.split_whitespace() {
      if count < k {
        if count != 0 {
          ret.push_str(" ");
        }
        ret.push_str(c);
        count += 1;
      }
    }
    return ret;
  }
}

fn main() {
  let test = String::from("Hello how are you Contestant");
  println!("{}", Solution::truncate_sentence(test, 4));
}